var orgData = _appConfigs.getUserData().userOrgs;
$(function(){
	var tableNames = ["dict_gender","dict_education","dict_knry","dict_difficultytype","dict_removereason"];
	for(var i=0;i<tableNames.length;i++){
		var id="#difficultyUpdate_"+tableNames[i];
		$(id).combobox({
			data:$.appCache.get(tableNames[i]),
			valueField:'code',    
		    textField:'name',
		    editable:false,
		    panelMaxHeight:90,
		});
	}
	
	$("#DifficultySearch_street").combobox({
		data:$.appCache.get("dict_street"),
		valueField:'code',    
	    textField:'name',
	    editable:false,
	    panelMaxHeight:90,
	    onSelect: function(record){
	        $("#DifficultySearch_community").combobox({
	          url:'application/dict/getSubMenu',
	          queryParams:{
	            tableName:'dict_community',
	            parent_code:record.code
	          },
	          valueField:'code',    
	          textField:'name',
	          missingMessage:'必填',
	          editable:false,
	          panelMaxHeight:90
	        }); 
	      }
	});
	
	$("#difficultyUpdate_dict_street").combobox({
		data:$.appCache.get("dict_street"),
		valueField:'code',    
	    textField:'name',
	    editable:false,
	    panelMaxHeight:90,
	    onSelect: function(record){
	        $("#difficultyUpdate_dict_community").combobox({
	          url:'application/dict/getSubMenu',
	          queryParams:{
	            tableName:'dict_community',
	            parent_code:record.code
	          },
	          valueField:'code',    
	          textField:'name',
	          missingMessage:'必填',
	          editable:false,
	          panelMaxHeight:90
	        }); 
	      }
	});
});
/**
 * 默认街道选择
 */
if(orgData.porgId == _appConfigs.appOrgId){
	 $('#DifficultySearch_street').combobox({disabled:true});
	 $('#DifficultySearch_street').combobox('select',orgData.orgId);
	 //$('#difficultyUpdate_dict_street').combobox({readonly:true});
	 $('#difficultyUpdate_dict_street').combobox({disabled:true});
	 $('#difficultyUpdate_dict_street').combobox('select',orgData.orgId);
}else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){
	$('#DifficultySearch_street').combobox({disabled:true});
	$('#DifficultySearch_community').combobox({disabled:true});
	$('#DifficultySearch_street').combobox('select',orgData.porgId);
	$('#DifficultySearch_community').combobox('select',orgData.orgId);
	$('#difficultyUpdate_dict_street').combobox({readonly:true});
	//$('#difficultyUpdate_dict_street').combobox({disabled:true});
	$('#difficultyUpdate_dict_community').combobox({readonly:true});
	//$('#difficultyUpdate_dict_community').combobox({disabled:true});
	$('#difficultyUpdate_dict_street').combobox('select',orgData.porgId);
	$('#difficultyUpdate_dict_community').combobox('select',orgData.orgId);
}else{
	$("#DifficultySearch_community").combobox({
		  data:$.appCache.get('dict_community'),
		  valueField:'code',    
		    textField:'name',
		    missingMessage:'必填',
		    editable:false,
		    panelMaxHeight:90
	}); 
	$("#difficultyUpdate_dict_community").combobox({
		  data:$.appCache.get('dict_community'),
		  valueField:'code',    
		    textField:'name',
		    missingMessage:'必填',
		    editable:false,
		    panelMaxHeight:90
	}); 
}
$('#difficultyInfoGrid').datagrid({
	url:'jobless/management/getDifficultyList', 
	queryParams:{
		street: $('#DifficultySearch_street').combobox('getValue'),
		community: $('#DifficultySearch_community').combobox('getValue'),
	},
	idField:'unemployedId',
	rownumbers:true,
    fitColumns:true,
    singleSelect:true,
    pagination:true,
    pagePosition:'bottom',
    columns:[[
              {field:'unemployedId',title:'主键',hidden:'true'}, 
              {field:'name',title:'姓名',width:90,align:'center'}, 
              {field:'sex',title:'性别',width:60,align:'center',
            	     formatter:function(value){
            	    	 var dict = $.appCache.get('dict_gender');
           		         if(!dict|| !dict.length) {
           		        	 return value;
           		         } else {
           		        	 var i = 0;
               		         for(i; i<dict.length; i++) {
               		        	 var data = dict[i];
               			         if(value == data.code) {
               			        	 return data.name;
               			         }
               		         }
           		         }
           	         }},  
              {field:'idcard',title:'身份证号',width:130,align:'center'},
              {field:'phone',title:'联系方式',width:100,align:'center'}, 
              {field:'education',title:'学历',width:80,align:'center',
            	     formatter:function(value){
            	    	 var dict = $.appCache.get('dict_education');
     		             if(!dict || !dict.length) {
     		            	 return value;
     		             } else {
     		            	 var i = 0;
         		             for(i; i<dict.length; i++) {
         		            	 var data = dict[i];
         			             if(value == data.code) {
         			            	 return data.name;
         			             }
         		             }
     		             }
     	             }},  
        	  {field:'unemploymentdate',title:'失业时间',width:90,align:'center',
        	    	 formatter:function(data){ 
        	    		 if(data!=null){
        	    			 var df = new Date(data);  
                             return df.format("yyyy-MM-dd");       			   
            		     } else{
            		    	 return null;
            		     }
            	     }},  
              {field:'knrydate',title:'转困难人员时间',width:110,align:'center',
            	     formatter:function(data){ 
            	    	 if(data!=null){
            	    		 var df = new Date(data);  
                             return df.format("yyyy-MM-dd");       			   
                		 } else{
                			 return null;
                		 }
                	 }}, 
              {field:'health',title:'健康状况',width:90,align:'center',hidden:true}, 
              {field:'difficultytype',title:'困难类型',width:90,align:'center',
            	     formatter:function(value){
            	    	 var dict = $.appCache.get('dict_difficultytype');
        		         if(!dict || !dict.length) {
        		        	 return value;
        		         } else {
        		        	 var i = 0;
            		         for(i; i<dict.length; i++) {
            		        	 var data = dict[i];
            			         if(value == data.code) {
            			        	 return data.name;
            			         }
            		         }
        		         }
        	         }},  
              {field:'trackingtime',title:'跟踪时间',hidden:'true'},	  
              {field:'removereason',title:'减少原因',width:110,align:'center',
            	     formatter:function(value){
            	    	 var dict = $.appCache.get('dict_removereason');
        		         if(!dict || !dict.length) {
        		        	 return value;
        		         } else {
        		        	 var i = 0;
            		         for(i; i<dict.length; i++) {
            		        	 var data = dict[i];
            			         if(value == data.code) {
            			        	 return data.name;
            			         }
            		         }
        		         }
        	         }},   
              {field:'removedate',title:'减少时间',width:90,align:'center',
           	       formatter:function(data){
           	    	   if(data!=null){
           	    		   var df = new Date(data);  
                             return df.format("yyyy-MM-dd");       			   
      		           } else{
      		        	   return null;
      		           }
      	           }},  
              {field:'street',title:'街道',width:100,align:'center',
      	        	 formatter:function(value){
      	        		 var dict = $.appCache.get('dict_street');
           		         if(!dict || !dict.length) {
           		        	 return value;
           		         } else {
           		        	 var i = 0;
               		         for(i; i<dict.length; i++) {
               		        	 var data = dict[i];
               			         if(value == data.code) {
               			        	 return data.name;
               			         }
               		         }
           		         }
           	         }},     
              {field:'community',title:'社区',width:120,align:'center',
           	         formatter:function(value){
           	        	 var dict = $.appCache.get('dict_community');
              		     if(!dict || !dict.length) {
              		    	 return value;
              		     } else {
              		    	 var i = 0;
                  		     for(i; i<dict.length; i++) {
                  		    	 var data = dict[i];
                  			     if(value == data.code) {
                  			    	 return data.name;
                  			     }
                  		     }
              		     }
              	     }},   
              {field:'remark',title:'备注',width:80,align:'center'}
          ]]   
   
}); 

/**
 * 查询条件
 */
function difficultySearch(){
	$('#difficultyInfoGrid').datagrid({
		url:'jobless/management/getDifficultyList', 
		queryParams:{
			street: $('#DifficultySearch_street').combobox('getValue'),
			community: $('#DifficultySearch_community').combobox('getValue'),
			name: $('#DifficultySearch_name').val(),	
			idcard: $('#DifficultySearch_idCard').val()
		} 										
	});
	$('#difficultyInfoGrid').datagrid('unselectAll');
}

/**
 * 曾经是困难人员存在困难人员服务台账的人员查询
 */
function historyDifficultySearch(){
	$('#difficultyInfoGrid').datagrid({
		url:'jobless/management/getHistoryDifficultyList', 
		queryParams:{
			street: $('#DifficultySearch_street').combobox('getValue'),
			community: $('#DifficultySearch_community').combobox('getValue'),
			name: $('#DifficultySearch_name').val(),	
			idcard: $('#DifficultySearch_idCard').val()
	    } 					
	});
	$('#difficultyInfoGrid').datagrid('unselectAll');
}

/**
 * 回显困难人员信息
 */
function getDifficultyInfo(){
	var rowData = $('#difficultyInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		var curr_time = new Date();
		var strDate = curr_time.getFullYear()+"-"+(curr_time.getMonth()+1)+"-"+curr_time.getDate();
		$('#difficultyUpdate_dict_removereason').combobox({
			onSelect: function(record){
				//alert(record.code);
				if(record.code != ''){
					$('#difficultyUpdate_removedate').datebox('setValue',strDate);
				} else{
					$('#difficultyUpdate_removedate').datebox('setValue','');
				}
			}
		});
		
		$('#modifyDifficulty').window('open');
		$.ajax({
				url:'jobless/management/getJoblessInfo',
				type:'post',
				data:{
					unemployedId:rowData.unemployedId					
				},
				success: function (data) { 
					if(data==null){
						$('#modifyDifficultyForm').form('clear');
					}else{
						$('#modifyDifficultyForm').form('load',data);
					}
		        },
		        error:function(data){
		        	$('#modifyDifficultyForm').form('clear');
		        }
			});
		}
}

/**
 * 修改困难人员信息
 * 
 */
function modifyDifficultyInfo(){
//	$('#modifyDifficultyForm').form('submit',{
//		url:'jobless/management/updateDifficulty',
//		success: function(data){
//			data =  $.parseJSON(data);
//			if(data.success == 'true'){
//				$.messager.alert('提示','修改成功','info');
//				$('#modifyDifficulty').window('close');
//				$('#modifyDifficultyForm').form('clear');
//				difficultySearch();
//			}else{
//				$.messager.alert('提示',data.message,'info');
//				$('#modifyDifficulty').window('close');
//				$('#modifyDifficultyForm').form('clear');
//			}
//		}
//	});
	
	var flag = $('#modifyDifficultyForm').form('validate');
	if(flag==false){
		$.messager.alert('提示','表单填写不正确');
		return;
	}else{
		$.ajax({
			url:'jobless/management/updateDifficulty',
		    type:'post',
		    async: false,
		    data:$('#modifyDifficultyForm').serialize(),
		    success:function(data){
		    	data =  $.parseJSON(data);
			    if(data.success == 'true'){
			    	$.messager.alert('提示','保存成功','info');
				    $('#modifyDifficulty').window('close');
				    $('#modifyDifficultyForm').form('clear');
				    difficultySearch();
			    }else{
			    	$.messager.alert('提示',data.message,'info');
			    }
		    },
		    error:function(data){
		    	alert("ajax发生错误！");	
		    }
	    });
	}
}

/**
 * 就业援助记录台账
 */
function aidRecord(){
	var rowData = $('#difficultyInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		var isExists = window.mainpage.mainTabs.isExists('就业援助记录', 'jobless/difficultyServer/aidRecordMenu');
		//debugger;
		switch (isExists) {
		    case 0: 
		    	window.mainpage.mainTabs.createTab('就业援助记录','jobless/difficultyServer/aidRecordMenu',
		    		'', true, true, true, true, {name: 'bindData', params: rowData}); break;
		    case 1: 
		    	window.mainpage.mainTabs.createTab('就业援助记录','jobless/difficultyServer/aidRecordMenu',
		    		'', true, true, true, true, {name: 'bindData', params: rowData}); break;
		    case 2: 
		    	window.mainpage.mainTabs.jumpTab('就业援助记录', {name: 'bindData', params: rowData}); break;
		    default: break;
		}
	}
}

/**
 * 跟踪服务记录台账
 */
function trackingRecord(){
	var rowData = $('#difficultyInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		var isExists = window.mainpage.mainTabs.isExists('跟踪服务记录', 'jobless/difficultyServer/trackingRecordMenu');
		switch (isExists) {
		    case 0: 
		    	window.mainpage.mainTabs.createTab('跟踪服务记录','jobless/difficultyServer/trackingRecordMenu',
		    		'', true, true, true, true, {name: 'bindData', params: rowData}); break;
		    case 1: 
		    	window.mainpage.mainTabs.createTab('跟踪服务记录','jobless/difficultyServer/trackingRecordMenu',
		    		'', true, true, true, true, {name: 'bindData', params: rowData}); break;
		    case 2: 
		    	window.mainpage.mainTabs.jumpTab('跟踪服务记录', {name: 'bindData', params: rowData}); break;
		    default: break;
		}
	}
}

/**
 * 就业援助效果台账
 */
function aidEffect(){
	var rowData = $('#difficultyInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		var isExists = window.mainpage.mainTabs.isExists('就业援助效果', 'jobless/difficultyServer/aidEffectMenu');
		//debugger;
		switch (isExists) {
		    case 0: 
		    	window.mainpage.mainTabs.createTab('就业援助效果','jobless/difficultyServer/aidEffectMenu',
		    		'', true, true, true, true, {name: 'bindData', params: rowData}); break;
		    case 1: 
		    	window.mainpage.mainTabs.createTab('就业援助效果','jobless/difficultyServer/aidEffectMenu',
		    		'', true, true, true, true, {name: 'bindData', params: rowData}); break;
		    case 2: 
		    	window.mainpage.mainTabs.jumpTab('就业援助效果', {name: 'bindData', params: rowData}); break;
		    default: break;
		}
	}
}

function exportDifficultyInfoList(){
	var street,community;
	 if(orgData.porgId == _appConfigs.appOrgId){  //街道
		 street = orgData.orgId;
		 community = "";
	 }else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){ //社区
		 street = "";
		 community = orgData.orgId;
	 }else{  //全部
		 street = "";
		 community = "";
	 }
	 window.location.href ="jobless/excel/exportDifficultyInfoList?street="+street+"&community="+community;
}
